<template>
  <div>
    <el-dialog :title="title" v-model="dialogVisible" :close-on-click-modal="false" :before-close="closeDialog"
               v-on:open="loadData">
      <el-form :model="form" :rules="rules" :inline="true" ref="form" label-width="100px">
        <el-form-item label="所属中心" prop="siteId">
          <el-select v-model="form.siteId" style="width:170px;">
            <el-option
              v-for="item in siteList"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <!--<el-form-item label="项目简拼" prop="pinyin">
          <el-input v-model="form.pinyin"></el-input>
        </el-form-item>-->
        <el-form-item label="方案编号" prop="num">
          <el-input v-model="form.num"></el-input>
        </el-form-item>
        <el-form-item label="方案版本" prop="schemeVersion">
          <el-input v-model="form.schemeVersion"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="respo">
          <el-input v-model="form.respo"></el-input>
        </el-form-item>
        <el-form-item label="申办方" prop="bidder">
          <el-input v-model="form.bidder"></el-input>
        </el-form-item>
        <el-form-item label="所需样本量" prop="email">
          <el-input v-model="form.caseAmount" type="number"></el-input>
        </el-form-item>
        <el-form-item label="参与间隔(天)" prop="timeInterval">
          <el-input v-model="form.timeInterval" type="number"></el-input>
        </el-form-item>
        <el-form-item label="修订日期" prop="reversionDate">
          <el-date-picker
            v-model="form.reversionDate"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="筛选号位数" prop="filterNumLength">
          <el-select v-model="form.filterNumLength" placeholder="请选择筛选号位数">
            <el-option
              v-for="item in numLength"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设置周期" prop="cycles">
          <el-input v-model="form.cycles" type="number"></el-input>
        </el-form-item>
        <el-form-item label="按病房分组" prop="groupnum">
          <el-input v-model="form.groupnum" type="number"></el-input>
        </el-form-item>
        <!--<el-form-item label="受试者号位数" prop="subjectsNumLength">-->
        <!--<el-select v-model="form.subjectsNumLength" placeholder="请选择受试者号位数">-->
        <!--<el-option-->
        <!--v-for="item in numLength"-->
        <!--:label="item"-->
        <!--:value="item"-->
        <!--&gt;-->
        <!--</el-option>-->
        <!--</el-select>-->
        <!--</el-form-item>-->
        <el-form-item label="描述" prop="remark">
          <el-input type="textarea" v-model="form.remark" style="width:450px;"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="24" style="padding:10px;">
            <span style="color:red;float:left;">"*"为必填项。</span>
            <div class="pull-right">
              <el-button type="primary" @click.native.prevent="handleSubmit" :disabled="editClick">{{addBtnTxt}}</el-button>
              <el-button @click.native.prevent="handleReset">重置</el-button>
              <el-button @click.native.prevent="closeDialog">取消</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import http from '../../../api/http'

  var model = {
    siteId: '',
    name: '',
    pinyin: '',
    num: '',
    bidder: '',
    caseAmount: '',
    schemeVersion: '',
    reversionDate: '',
    telephone: '',
    respo: '',
    email: '',
    filterNumLength: 2,
    subjectsNumLength: 2,
    remark: '',
    timeInterval:90,
    status: 0
  };//状态码
  export default {
    data() {
      return {
        editClick:false,
        form: {
          filter: {kwd: '', sort: {create_datetime: 'desc'}, fieldList: [{field: 'status', exp: '=', val: ''}]},
          pageNo: 1,
          pageSize: 10
        },
        addBtnTxt: "",
        title: '项目添加', form: Object.assign({}, model),
        rules: {
          siteId: [
            {required: true, message: '中心必选'}
          ],
          name: [
            {required: true, message: '请输入项目名称'},
          ],
          /*pinyin: [
            {required: true, message: '请输入项目简称'},
            {min: 1, max: 6, message: '长度在 6 个字符以内'}
          ],*/
          respo: [{required: true, message: '请输入负责人'}],
          bidder: [{required: true, message: '请输入申办者'}],
          num: [{required: true, message: '请输入项目编号'}],
          cycles:[{required: true, message: '请输入周期'}],
          groupnum:[{required: true, message: '请输入组号'}],
        },
        radio: 1,
        siteList: [],
        // 编号长度
        numLength: [2, 3, 4]
      }
    },
    props: {
      dialogFormVisible: {
        type: Boolean,
        default: function () {
          return false
        }
      },
      siteId: {
        type: String
      },
      parent: {
        type: Object,
        default: function () {
          return {}
        }
      },
      id:{
          type:String
      }
    },
    computed: {
      dialogVisible: {
        get: function () {
          var vm = this;
          vm.projectList = vm.$parent.list;
          if (this.siteId != undefined) {
            this.form.siteId = this.siteId;
          }
          return this.dialogFormVisible
        },
        set: function () {
        }
      }
    },
    methods: {
      handleReset() {
        this.$refs.form.resetFields()
      },
      handleSubmit(ev) {
        var vm = this;
        this.$refs.form.validate(function (valid) {
          if (valid) {
            vm.editClick = true;
            vm.form.projectTypeId = vm.$parent.form.project_type_id;
            http.postData(vm, "/pt/project/save", {json: JSON.stringify(vm.form)}, function (resp) {
              var ret = resp.body;
              if (ret.code === '111') {
                vm.$notify({title: '操作提示', message: ret.msg, type: 'success'});
                vm.form = Object.assign({}, model);
                vm.$router.push({name: 'P010201', query: {pushSiteId: vm.siteId}});
                vm.closeDialog();
                vm.$parent.onSubmit()
              } else {
                vm.editClick = false;
                vm.$notify.error({title: '操作失败', message: ret.msg, type: 'error'});
              }
            });
          } else {
            return false
          }
        })
      }, closeDialog() {
        this.$emit('closeDialog');
        this.form= Object.assign({}, model);
        this.editClick = false;
      }, loadData() {
        var vm = this;
        if (this.id != undefined && this.id != '') {
          http.postData(vm, "/pt/project/detail/" + this.id, {}, function (resp) {
            var ret = resp.body;
            if (ret.code === '111') {
              vm.form = ret.value;
            } else {
              vm.$notify.error({title: '操作失败', message: ret.msg});
            }
          });
        };
//          if (ret != undefined) {
//            vm.form = ret;
//            if (vm.form.siteId == undefined || vm.form.siteId == '') {
//              vm.form.siteId = '';
//            }
//          } else {
//            vm.form = Object.assign({}, model);
//          }
          if (vm.id == undefined || vm.id == '') {
            vm.addBtnTxt = '立即创建';
            vm.title = '项目创建';
          } else {
            vm.addBtnTxt = '保存修改';
            vm.title = "项目修改";
          }

        //获取中心
        if(this.$utils.getRoleCodeList().indexOf('SITE_MANAGER')==-1){
          http.postData(vm, '/pt/site/list', {}, function (res) {
            if (res.body.code == '111')
              vm.siteList = res.body.value;
          });
        }else{
          var user=this.$utils.getCurrenrUserInfo();
          vm.siteList=[{id:user.siteId,name:user.siteName}];
          vm.form.siteId=user.siteId;
        }
      }
    }
  }
</script>
